BUILD_TOOL = yarn


build: ; $(info ============================== 编译 ==============================)
	$(BUILD_TOOL) build-only 

dev: ; $(info ============================== 运行 ==============================)
	$(BUILD_TOOL) run dev

# ============================================ Docker 相关指令 ==========================================================


# 镜像版本
version = $(shell git describe --tags --always --dirty)

REGISTRY = 192.168.0.215:37080/public/
# 镜像名称
IMAGE_NAME = de_asset_manager_service_web_ui
# 镜像全名
IMAGE_FULL_NAME = $(REGISTRY)$(IMAGE_NAME):$(version)
# 镜像导出文件名
IMAGE_OUTPUT_FILE_NAME = docker_image_$(IMAGE_NAME)_$(version).tar

docker-build: ; $(info ============================== 构建镜像 ==============================)
	docker build --platform linux/x86_64 --pull=false  -t $(IMAGE_FULL_NAME) -f Dockerfile .

docker-save: ; $(info ============================== 导出镜像 ==============================)
	docker save -o dist_image/$(IMAGE_OUTPUT_FILE_NAME) $(IMAGE_FULL_NAME)

docker-image-clean: ; $(info ============================== 清理本地历史构建镜像 ==============================)
	docker images | grep -E "^$(IMAGE_NAME)[[:space:]]+" | awk '{print $3}' | xargs docker rmi

ARM_IMAGE_PREFIX = arm64_
ARM_IMAGE_FULL_NAME = $(ARM_IMAGE_PREFIX)$(IMAGE_NAME):$(version)
ARM_IMAGE_OUTPUT_FILE_NAME = docker_image_$(ARM_IMAGE_PREFIX)$(IMAGE_NAME)_$(version).tar
arm-docker-build: ; $(info ============================== 构建开发镜像 ==============================)
	docker build --platform linux/arm64 --pull=false  -t $(ARM_IMAGE_FULL_NAME) -f Dockerfile.arm .
arm-docker-save: ; $(info ============================== 导出开发镜像 ==============================)
	docker save -o dist_image/$(ARM_IMAGE_OUTPUT_FILE_NAME) $(ARM_IMAGE_FULL_NAME)

arm-docker-image-clean: ; $(info ============================== 清理本地历史构建镜像 ==============================)
	docker images | grep -E "^$(ARM_IMAGE_PREFIX)$(IMAGE_NAME)[[:space:]]+" | awk '{print $3}' | xargs docker rmi

# ============================================ 一键发布指令 =============================================================

deploy-dev: build docker-build docker-save ssh-push-harbor ssh-k8s-apply ssh-docker-clean
release-arm: build arm-docker-build arm-docker-save
# ============================================ SSH 相关指令 =============================================================

# 测试主机地址
#DEV_HOST = 192.168.0.222
DEV_HOST = 27.115.98.190
# 测试主机端口
DEV_HOST_PORT = 12022
# 测试主机用户名
DEV_HOST_USER = root

ssh-push-harbor: ; $(info ============================== 推送并运行镜像[开发] ==============================)
	scp -P $(DEV_HOST_PORT) dist_image/$(IMAGE_OUTPUT_FILE_NAME) $(DEV_HOST_USER)@$(DEV_HOST):/tmp/
	ssh -p $(DEV_HOST_PORT) $(DEV_HOST_USER)@$(DEV_HOST) "docker load -i /tmp/$(IMAGE_OUTPUT_FILE_NAME)"
	ssh -p $(DEV_HOST_PORT) $(DEV_HOST_USER)@$(DEV_HOST) "docker push $(IMAGE_FULL_NAME)"

ssh-k8s-apply:
	ssh -p $(DEV_HOST_PORT) $(DEV_HOST_USER)@$(DEV_HOST) "TAG=$(version) envsubst < k8s-config/$(IMAGE_NAME).yaml | kubectl apply -f-"

DEPLOY_NAME = asset-manager-web-ui

last-version:
	ssh -p $(DEV_HOST_PORT) $(DEV_HOST_USER)@$(DEV_HOST) "kubectl get deployments.apps $(DEPLOY_NAME) -o go-template='{{ (index .spec.template.spec.containers 0 ).image}}' | awk -F '[:]' '{print $$NF}'"

ssh-docker-clean: ; $(info ============================== clean docker build images ==============================)
	ssh -p $(DEV_HOST_PORT) $(DEV_HOST_USER)@$(DEV_HOST) "docker rmi $(IMAGE_FULL_NAME)"


